String.prototype.render = function(context) {
	var tokenReg = /(\\)?\{([^\{\}\\]+)(\\)?\}/g;

	return this.replace(tokenReg, function(word, slash1, token, slash2) {
		if (slash1 || slash2) {
			return word.replace('\\', '');
		}

		var variables = token.replace(/\s/g, '').split('.');
		var currentObject = context;
		var i, length, variable;

		for (i = 0, length = variables.length; i < length; ++i) {
			variable = variables[i];
			currentObject = currentObject[variable];
			if (currentObject === undefined || currentObject === null) return '';
		}
		return currentObject;
	});
};
var t = 1;
var re = /x/;
console.log(re);
re.toString = function() {
	showMessage('哈哈，你打开了控制台，是想要看看我的秘密吗？', 5000, true);
	return '';
};

$(document).on('copy', function() {
	showMessage('你都复制了些什么呀，发给别人要记得推荐我呀~', 5000, true);
});

$(document).on("visibilitychange", function() {
	if (!document.hidden) showMessage("(◍'౪`◍)ﾉﾞ欢迎回来 ! ^_^o", 6000, 9);
});

// $('#hitokoto').mouseover(function (){
//     var text = '这句一言出处是 <span style="color:#0099cc;">『{source}』</span>，是 <span style="color:#0099cc;">FGHRSH</span> 在 {date} 收藏的！';
//     var hitokoto = JSON.parse($(this)[0].dataset.raw);
//     text = text.render({source: hitokoto.source, author: hitokoto.author, date: hitokoto.date});
//     showMessage(text, 3000);
// });

$('.waifu-tool .icon-shouye').click(function() {
	window.location = '#';
	// window.location = $('#Header1_HeaderTitle').attr("href")
});

$('.waifu-tool .icon-huanren').click(function() {
	loadOtherModel();
});

$('.waifu-tool .icon-xiaoxi').click(function() {
	showHitokoto();
});

$('.waifu-tool .icon-huanzhuang').click(function() {
	loadRandModel();
	showMessage('我的新衣服好看嘛', 3000, true);
});

$('.waifu-tool .fui-info-circle').click(function() {
	window.open('http://bd.zhichi921.com/xiaomi');
});

$('.waifu-tool .icon-tuichu').click(function() {
	sessionStorage.setItem('waifu-dsiplay', 'none');
	showMessage('愿你有一天能与重要的人重逢，再见啦~', 3000, true);
	window.setTimeout(function() {
		$('.waifu').hide();
	}, 3000);
});

$('.waifu-tool .icon-paizhao').click(function() {
	showMessage('照好了嘛，是不是很可爱呢？', 5000, true);
	window.Live2D.captureName = 'Live2D.png';
	window.Live2D.captureFrame = true;
});

/* function waifuWelcome(){
    var text;
    // var SiteIndexUrl = 'http://bd.zhichi921.com/xiaomi/';  // 手动指定主页
    var SiteIndexUrl = window.location.protocol+'//'+window.location.hostname+'/';  // 自动获取主页

    if (window.location.href == SiteIndexUrl) {      // 如果是主页
        var now = (new Date()).getHours();
        if (now > 23 || now <= 5) {
            text = '你是夜猫子呀？这么晚还不睡觉，明天起的来嘛~';
        } else if (now > 5 && now <= 7) {
            text = '早上好！一日之计在于晨，美好的一天就要开始了~';
        } else if (now > 7 && now <= 11) {
            text = '上午好！工作顺利嘛，不要久坐，多起来走动走动哦！';
        } else if (now > 11 && now <= 14) {
            text = '中午了，工作了一个上午，现在是午餐时间！';
        } else if (now > 14 && now <= 17) {
            text = '午后很容易犯困呢，今天的运动目标完成了吗？';
        } else if (now > 17 && now <= 19) {
            text = '傍晚了！窗外夕阳的景色很美丽呢，最美不过夕阳红~';
        } else if (now > 19 && now <= 21) {
            text = '晚上好，今天过得怎么样？';
        } else if (now > 21 && now <= 23) {
            text = '已经这么晚了呀，早点休息吧，晚安，明天见~';
        } else {
            text = '嗨~ 快来逗我玩吧！';
        }
    } else {
        if(document.referrer !== ''){
            var referrer = document.createElement('a');
            referrer.href = document.referrer;
            var domain = referrer.hostname.split('.')[1];
            if (window.location.hostname == referrer.hostname) {
                var now = (new Date()).getHours();
                var tips = ''
                if (now > 23 || now <= 5) {
                    tips = '你是夜猫子呀？这么晚还不睡觉，明天起的来嘛~';
                } else if (now > 5 && now <= 7) {
                    tips = '早上好！一日之计在于晨，美好的一天就要开始了~';
                } else if (now > 7 && now <= 11) {
                    tips = '上午好！工作顺利嘛，不要久坐，多起来走动走动哦！';
                } else if (now > 11 && now <= 14) {
                    tips = '中午了，工作了一个上午，现在是午餐时间！';
                } else if (now > 14 && now <= 17) {
                    tips = '午后很容易犯困呢，今天的运动目标完成了吗？';
                } else if (now > 17 && now <= 19) {
                    tips = '傍晚了！窗外夕阳的景色很美丽呢，最美不过夕阳红~';
                } else if (now > 19 && now <= 21) {
                    tips = '晚上好，今天过得怎么样？';
                } else if (now > 21 && now <= 23) {
                    tips = '已经这么晚了呀，早点休息吧，晚安，明天见~';
                } else {
                    tips = '嗨~ 快来逗我玩吧！';
                }
                text = '欢迎来到<span style="color:#0099cc;">『' + document.title.split(' - ')[0] + '』</span>'+ '<br>' + tips;
            } else if (domain == 'baidu') {
                text = 'Hello! 来自 百度搜索 的朋友<br>你是搜索 <span style="color:#0099cc;">' + referrer.search.split('&wd=')[1].split('&')[0] + '</span> 找到的我吗？';
            } else if (domain == 'so') {
                text = 'Hello! 来自 360搜索 的朋友<br>你是搜索 <span style="color:#0099cc;">' + referrer.search.split('&q=')[1].split('&')[0] + '</span> 找到的我吗？';
            } else if (domain == 'google') {
                text = 'Hello! 来自 谷歌搜索 的朋友<br>欢迎来到<span style="color:#0099cc;">『' + document.title.split(' - ')[0] + '』</span>';
            } else {
                text = 'Hello! 来自 <span style="color:#0099cc;">' + referrer.hostname + '</span> 的朋友';
            }
        } else {
            text = '欢迎来到<span style="color:#0099cc;">『' + document.title.split(' - ')[0] + '』</span>';
        }
    }
    showMessage(text, 6000);
} */
function waifuWelcome() {
	var text;
	var tips = ''
	var now = (new Date()).getHours();
	if (now > 23 || now <= 5) {
		tips = '你是夜猫子呀？这么晚还不睡觉，明天起的来嘛~';
	} else if (now > 5 && now <= 7) {
		tips = '早上好！一日之计在于晨，美好的一天就要开始了~';
	} else if (now > 7 && now <= 11) {
		tips = '上午好！工作顺利嘛，不要久坐，多起来走动走动哦！';
	} else if (now > 11 && now <= 14) {
		tips = '中午了，工作了一个上午，现在是午餐时间！';
	} else if (now > 14 && now <= 17) {
		tips = '午后很容易犯困呢，今天的运动目标完成了吗？';
	} else if (now > 17 && now <= 19) {
		tips = '傍晚了！窗外夕阳的景色很美丽呢，最美不过夕阳红~';
	} else if (now > 19 && now <= 21) {
		tips = '晚上好，今天过得怎么样？';
	} else if (now > 21 && now <= 23) {
		tips = '已经这么晚了呀，早点休息吧，晚安，明天见~';
	} else {
		tips = '嗨~ 快来逗我玩吧！';
	}
	text = '欢迎来到<span style="color:#0099cc;">『' + document.title.split(' - ')[0] + '』</span>' + '<br>' + tips;
	showMessage(text, 6000);
}

window.hitokotoTimer = window.setInterval(showHitokoto, 3000);
/* 检测用户活动状态，并在空闲时 定时显示一言 */
var getActed = true;
// window.hitokotoTimer = 0;
var hitokotoInterval = true;

$(document).mousemove(function(e) {
	getActed = true;
}).keydown(function() {
	getActed = true;
});
setInterval(function() {
	if (!getActed) ifActed();
	else elseActed();
}, 1000);

function ifActed() {
	if (!hitokotoInterval) {
		hitokotoInterval = true;
		hitokotoTimer = window.setInterval(showHitokoto, 10000);
	}
}

function elseActed() {
	getActed = hitokotoInterval = false;
	window.clearInterval(hitokotoTimer);
}

function showHitokoto() {
	/* 增加 hitokoto.cn API */

	window.clearTimeout(t);
	$.getJSON('https://v1.hitokoto.cn', function(result) {
		var text = '这句一言出自 <span style="color:#0099cc;">『{source}』</span>'; //，是 <span style="color:#0099cc;">{creator}</span> 在 hitokoto.cn 投稿的。';
		text = text.render({
			source: result.from
		}); //, creator: result.creator
		showMessage(result.hitokoto, 5000);
		t = window.setTimeout(function() {
			showMessage(text, 3000);
		}, 5000);

	});
}

function showMessage(text, timeout, flag) {
	if (flag || sessionStorage.getItem('waifu-text') === '' || sessionStorage.getItem('waifu-text') === null) {
		if (Array.isArray(text)) text = text[Math.floor(Math.random() * text.length + 1) - 1];
		//console.log(text);

		if (flag) sessionStorage.setItem('waifu-text', text);

		$('.waifu-tips').stop();
		$('.waifu-tips').html(text).fadeTo(200, 1);
		if (timeout === undefined) timeout = 5000;
		hideMessage(timeout);
	}
}

function hideMessage(timeout) {
	$('.waifu-tips').stop().css('opacity', 1);
	if (timeout === undefined) timeout = 5000;
	window.setTimeout(function() {
		sessionStorage.removeItem('waifu-text')
	}, timeout);
	$('.waifu-tips').delay(timeout).fadeTo(200, 0);
}


var waifuJson = {
	"mouseover": [{
			"selector": "a[href^='http'] h3",
			"text": ["要看看 <span style=\"color:#0099cc;\">{text}</span> 吗？"]
		},
		{
			"selector": ".nav",
			"text": ["要看看 <span style=\"color:#0099cc;\">{text}</span> 吗？"]
		},
		{
			"selector": ".search_box input",
			"text": ["在这里输入你想搜索的内容，点击搜索就可以出来了哦~"]
		},
		{
			"selector": ".search_word",
			"text": ["是想要搜索 <span style=\"color:#0099cc;\">{text}</span> 吗？"]
		},
		{
			"selector": "#to_top",
			"text": ["点击就能回到我们最初相遇的地方~<br>(～￣▽￣)～"]
		},
		{
			"selector": "#qq",
			"text": ["这里是主人的QQ，点击就能找到他啦~<br>(๑•̀ㅂ•́)و✧"]
		},
		{
			"selector": "#weixin,.wechat",
			"text": ["这里是主人的微信，我知道你想加他，快去扫码吧~<(￣ˇ￣)/"]
		},
		{
			"selector": "#fankui",
			"text": ["这里是反馈区，有什么意见或者建议的话，都可以写出来。我想听听你的想法(o゜▽゜)o☆~"]
		},
		{
			"selector": "#textarea",
			"text": ["请务必认真填写哦，感谢~"]
		},
		{
			"selector": "#rata",
			"text": ["给我打个分吧~<(￣ˇ￣)/"]
		},
		{
			"selector": "#submit",
			"text": ["提交之后请稍等片刻，请不要重复提交哦~"]
		},
		{
			"selector": ".tools a",
			"text": ["{text}"]
		},
		{
			"selector": ".ganhuolist a",
			"text": ["要看看 <span style=\"color:#0099cc;\">{text}</span> 吗？"]
		},
		{
			"selector": ".recommend",
			"text": ["有什么更好的网站推荐吗？"]
		},
		{
			"selector": "#lofter",
			"text": ["这里是主人的LOFTER，要去看看吗？"]
		},
		{
			"selector": "#mayun",
			"text": ["这里是主人的码云，要去看看吗？"]
		},
		{
			"selector": "#htmer_time",
			"text": ["已经过了这么久了呀，日子过得好快呢", "<span style=\"color:#0099cc;\">2010</span> 是多久来着…<br>1，2，3……有好多好多天呢！"]
		},
		{
			"selector": ".updatelog",
			"text": ["这里记录着我搬家的历史呢"]
		},
		{
			"selector": ".donate-qrcode",
			"text": ["主人最近在吃土呢，很辛苦的样子。给他一些钱钱吧~"]
		},
		{
			"selector": "#wenkmPlayer div.switch-player",
			"text": ["想要听点音乐吗"]
		},
		{
			"selector": "#wenkmPlayer div.volume",
			"text": ["在这里可以调整<span style=\"color:#0099cc;\">音量</span>呢"]
		},
		{
			"selector": "#wenkmPlayer div.switch-playlist",
			"text": ["<span style=\"color:#0099cc;\">播放列表</span>里都有什么呢"]
		},
		{
			"selector": "#wenkmPlayer div.switch-ksclrc",
			"text": ["有<span style=\"color:#0099cc;\">歌词</span>的话就能跟着一起唱呢"]
		},
		{
			"selector": "#navbar-page-116",
			"text": ["要去大佬们的家看看吗？"]
		},
		{
			"selector": "#navbar-page-34",
			"text": ["有什么想说的吗？"]
		},
		{
			"selector": "navbar-page-97",
			"text": ["这里都是主人的黑历史呢"]
		},
		{
			"selector": "#navbar-page-31",
			"text": ["这里有一些关于我家主人的秘密哦，要不要看看呢"]
		},
		{
			"selector": ".waifu-tool .fui-home",
			"text": ["回首页看看吧！"]
		},
		{
			"selector": ".waifu-tool .fui-eye",
			"text": ["要切换看板娘吗？"]
		},
		{
			"selector": ".waifu-tool .fui-chat",
			"text": ["猜猜我要说些什么？"]
		},
		{
			"selector": ".waifu-tool .fui-user",
			"text": ["喜欢换装PLAY吗？"]
		},
		{
			"selector": ".waifu-tool .fui-info-circle",
			"text": ["想要知道更多有关我的事吗？"]
		},
		{
			"selector": ".waifu-tool .fui-cross",
			"text": ["到了要说再见的时候了吗~"]
		},
		{
			"selector": ".waifu-tool .fui-photo",
			"text": ["你要给我拍照呀，一二三~茄子~~"]
		},
		{
			"selector": ".waifu #live2d",
			"text": ["干嘛呢你，快把手拿开", "鼠…鼠标放错地方了！"]
		}
	],
	"click": [{
		"selector": ".waifu #live2d",
		"text": ["是…是不小心碰到了吧！(＞﹏＜)", "萝莉控是什么呀！(⊙ｏ⊙)", "你看到我的小熊了吗？ค(TㅅT)", "再摸的话我可要报警了！⌇●﹏●⌇", "110吗，这里有个变态一直在摸我<br>(ó﹏ò｡)"]
	}],
	"seasons": [{
			"date": "01/01",
			"text": "<span style=\"color:#0099cc;\">元旦</span>了呢，新的一年又开始了，今年是{year}年~"
		},
		{
			"date": "02/09",
			"text": "今天第一天上线，值得纪念的日子呢~"
		},
		{
			"date": "02/14",
			"text": "又是一年<span style=\"color:#0099cc;\">情人节</span>，{year}年找到对象了嘛~"
		},
		{
			"date": "03/08",
			"text": "今天是<span style=\"color:#0099cc;\">妇女节</span>！"
		},
		{
			"date": "03/12",
			"text": "今天是<span style=\"color:#0099cc;\">植树节</span>，要保护环境呀！"
		},
		{
			"date": "04/01",
			"text": "悄悄告诉你一个秘密~<span style=\"background-color:#34495e;\">今天是愚人节，不要被骗了哦~</span>"
		},
		{
			"date": "05/01",
			"text": "今天是<span style=\"color:#0099cc;\">五一劳动节</span>，计划好假期去哪里了吗~"
		},
		{
			"date": "06/01",
			"text": "<span style=\"color:#0099cc;\">儿童节</span>了呢，快活的时光总是短暂，要是永远长不大该多好啊…"
		},
		{
			"date": "09/03",
			"text": "<span style=\"color:#0099cc;\">中国人民抗日战争胜利纪念日</span>，铭记历史、缅怀先烈、珍爱和平、开创未来。"
		},
		{
			"date": "09/10",
			"text": "<span style=\"color:#0099cc;\">教师节</span>，在学校要给老师问声好呀~"
		},
		{
			"date": "10/01",
			"text": "<span style=\"color:#0099cc;\">国庆节</span>，新中国已经成立71年了呢"
		},
		{
			"date": "11/05-11/12",
			"text": "今年的<span style=\"color:#0099cc;\">双十一</span>是和谁一起过的呢~"
		},
		{
			"date": "12/20-12/31",
			"text": "这几天是<span style=\"color:#0099cc;\">圣诞节</span>，主人肯定又去剁手买买买了~"
		}
	]
};
function initModel() {
	waifuWelcome();

	var modelId = localStorage.getItem('modelId');
	var modelTexturesId = localStorage.getItem('modelTexturesId');

	if (modelId == null) {

		/* 首次访问加载 指定模型 的 指定材质 */

		var modelId = 5; // 模型 ID
		var modelTexturesId = 0; // 材质 ID

	}
	loadModel(modelId, modelTexturesId);


	$.each(waifuJson.mouseover, function(index, tips) {
		$(document).on("mouseover", tips.selector, function() {
			var text = tips.text;
			if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length + 1) - 1];
			text = text.render({
				text: $(this).text()
			});
			showMessage(text, 3000);
		});
	});
	$.each(waifuJson.click, function(index, tips) {
		$(document).on("click", tips.selector, function() {
			var text = tips.text;
			if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length + 1) - 1];
			text = text.render({
				text: $(this).text()
			});
			showMessage(text, 3000, true);
		});
	});
	$.each(waifuJson.seasons, function(index, tips) {
		var now = new Date();
		var after = tips.date.split('-')[0];
		var before = tips.date.split('-')[1] || after;

		if ((after.split('/')[0] <= now.getMonth() + 1 && now.getMonth() + 1 <= before.split('/')[0]) &&
			(after.split('/')[1] <= now.getDate() && now.getDate() <= before.split('/')[1])) {
			var text = tips.text;
			if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length + 1) - 1];
			text = text.render({
				year: now.getFullYear()
			});
			showMessage(text, 6000, true);
		}
	});
}

function loadModel(modelId, modelTexturesId) {
	localStorage.setItem('modelId', modelId);
	if (modelTexturesId === undefined) modelTexturesId = 0;
	localStorage.setItem('modelTexturesId', modelTexturesId);
	loadlive2d('live2d', '//live2d.fghrsh.net/api/get/?id=' + modelId + '-' + modelTexturesId, console.log('live2d', '模型 ' +
		modelId + '-' + modelTexturesId + ' 加载完成'));
}

function loadRandModel() {
	var modelId = localStorage.getItem('modelId');
	var modelTexturesId = localStorage.getItem('modelTexturesId');

	var modelTexturesRandMode = 'switch'; // 可选 'rand'(随机), 'switch'(递增)

	$.ajax({
		cache: false,
		url: '//live2d.fghrsh.net/api/' + modelTexturesRandMode + '_textures/?id=' + modelId + '-' + modelTexturesId,
		dataType: "json",
		success: function(result) {
			loadModel(modelId, result.textures['id']);
		}
	});
}

function loadOtherModel() {
	var modelId = localStorage.getItem('modelId');

	var modelTexturesRandMode = 'switch'; // 可选 'rand'(随机), 'switch'(递增)

	$.ajax({
		cache: false,
		url: '//live2d.fghrsh.net/api/' + modelTexturesRandMode + '/?id=' + modelId,
		dataType: "json",
		success: function(result) {
			loadModel(result.model['id']);
			showMessage(result.model['message'], 3000, true);
		}
	});
}